package com.jade.store.wechat.service.impl;

import com.jade.bss.DbException;
import com.jade.bss.base.utils.CopyBean;
import com.jade.bss.db.impl.BaseFacadeImpl;
import com.jade.framework.base.criterion.Criteria;
import com.jade.framework.base.criterion.Restrictions;
import com.jade.framework.base.util.PagingList;
import com.jade.framework.base.util.PagingResult;
import com.jade.store.wechat.dao.WechatMsgDao;
import com.jade.store.wechat.entity.WechatMsg;
import com.jade.store.wechat.service.WechatMsgFacade;
import com.jade.store.wechat.vo.WechatMsgVo;
import com.jade.zsy.base.entity.Sku;
import com.jade.zsy.base.vo.SkuVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import javax.inject.Inject;
import javax.inject.Named;

/**
 * Created with IntelliJ IDEA.
 * User: JinYa 2009_jinya@163.com
 * Date: 2016-5-5
 * Time: 10:53
 * Version : 1.0
 * Remark:
 * History:
 */
@Service("WechatMsgFacade")
public class WechatMsgFacadeImpl extends BaseFacadeImpl implements WechatMsgFacade {
    @Inject
    @Named("WechatMsgDao")
    private WechatMsgDao wechatMsgDao;
    @Override
    public PagingResult queryEntity(Object obj, int page, int size, String keys, String... orders) throws DbException,IllegalAccessException, InstantiationException {
        WechatMsgVo entity=(WechatMsgVo)obj;
        Criteria criteria = new Criteria((page - 1) * size, size);
        if(StringUtils.isNotEmpty(keys)){
            criteria.add(Restrictions.or(Restrictions.like("skuCode", keys),Restrictions.like("barcode", keys)));
        }
        if(entity!=null &&StringUtils.isNotEmpty(entity.getOwnerId())){
            criteria.add(Restrictions.eq("ownerId", entity.getOwnerId()));
        }
        if(entity!=null && entity.getCrmId()>0){
            criteria.add(Restrictions.eq("crmId", entity.getCrmId()));
        }
        PagingList pagingList = wechatMsgDao.queryEntity(WechatMsg.class, criteria);
        PagingResult  result = PagingResult.getResult(CopyBean.copy(pagingList, WechatMsgVo.class), pagingList.getTotalCount(), page, size);
        return result;
    }
}
